<template>
	<view class="mainpadding2" style="padding-bottom: 150rpx;">
		<u-navbar title="員工管理" placeholder autoBack>
			<view class="flexright xiaohui" slot="right" @click="addStaff">添加</view>
		</u-navbar>
		<view class="flexcenter" v-if="!list.data.length">
			<view class="placeholderimage" style="margin-top: 100rpx;" ></view>
		</view>
		<view class="mainpadding_top ffffff radius dingwei" v-for="item in list.data" :key="item.id" @click="checkitem(item)">
			<view class="checkfocus" v-if="item.ischeck">
				<u-icon name="checkbox-mark" color="#fff" size="16"></u-icon>
			</view>
			<view class="flex">
				<view class="margin_left2">
					<view class="main_size hei_text bold">{{item.name}}</view>
					<view class="xiaohui margin_top1">服務區域：{{item.address}}</view>
				</view>
			</view>
			<view class="xiaohui margin_top pdl">
				員工權限：
				<text v-for="(value,i) in item.authority_arr" :key="value.id">
					{{value.name}}
					<text v-show="i!=item.authority_arr.length-1">/</text>
				</text>
			</view>
			<view class="flexright xiaohuang main_size bold margin_top">
				<view class="flexleft" @click="editStaff(item.id)">
					<u-icon name="edit-pen" color="#ff8505" size="16"></u-icon>
					<view class="xiaohei">編輯</view>
				</view>
				<view class="flexleft margin_left" @click="removeStaff(item.id)">
					<u-icon name="trash" color="#ff8505" size="16"></u-icon>
					<view class="xiaohei">刪除</view>
				</view>
			</view>
		</view>
		<view class="posibottom mainpadding" v-if="type==1">
			<view class="submitbtn" @click="submit">確認</view>
		</view>
		<!-- 刪除員工二次確認 -->
		<moDal :message="{modalShow:modal.modalShow,title:modal.title}" @modalCancel="modalCancel"
			@modalSure="modalSure"></moDal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				shop_id: uni.getStorageSync("shop_id"),
				modal: {
					modalShow: false,
					title: "您確定要刪除該員工嗎?"
				},
				common: [{
					id: 1,
					name: "資產管理",
					isSelect:false
				}, {
					id: 2,
					name: "店鋪管理",
					isSelect:false
				}, {
					id:3,
					name: "核銷記錄",
					isSelect:false
				},{
					id: 4,
					name: "優惠券管理",
					isSelect:false
				}, {
					id: 5,
					name: "免運券管理",
					isSelect:false
				}, {
					id: 6,
					name: "商品評價",
					isSelect:false
				}, {
					id: 7,
					name: "發票管理",
					isSelect:false
				}, {
					id: 8,
					name: "員工管理",
					isSelect:false
				}, {
					id: 9,
					name: "商品管理",
					isSelect:false
				}, {
					id: 10,
					name: "分配管理",
					isSelect:false
				}, {
					id: 11,
					name: "功能服務",
					isSelect:false
				}, {
					id: 12,
					name: "客戶管理",
					isSelect:false
				}, {
					id: 13,
					name: "商品統計",
					isSelect:false
				}, {
					id: 14,
					name: "物流管理",
					isSelect:false
				}, {
					id: 15,
					name: "收款管理",
					isSelect:false
				}, {
					id: 16,
					name: "工作站",
					isSelect:false
				}, {
					id: 17,
					name: "產品分類",
					isSelect:false
				}, {
					id: 18,
					name: "計算方式",
					isSelect:false
				}, {
					id: 19,
					name: "打印機管理",
					isSelect:false
				},{
					id: 20,
					name: "分店管理",
					isSelect:false
				},{
					id: 21,
					name: "分店付款",
					isSelect:false
				},{
					id: 22,
					name: "配送訂單",
					isSelect:false
				},{
					id: 23,
					name: "結算單",
					isSelect:false
				},{
					id: 24,
					name: "自動接單",
					isSelect:false
				}, {
					id: 25,
					name: "營業狀態",
					isSelect:false
				},{
					id: 26,
					name: "購買員工人數",
					isSelect:false
				},{
					id: 27,
					name: "叫號重置",
					isSelect:false
				},],
				list: {
					data: []
				},
				id: "", //要刪除的員工id
				data: {},
				type:0,//1分店調查選擇員工
				listids:[]
			}
		},
		onLoad(options) {
			if(options.msg){
				let data = JSON.parse(decodeURIComponent(options.msg));
				this.type = data.type
				this.listids = data.shopstaff
			}
		},
		onShow() {
			this.getList()
			this.get()
		},
		methods: {
			checkitem(item){
				if(this.type){
					item.ischeck = item.ischeck?false:true
				}
			},
			async get() {
				let res = await this.http.request('/api/shop/Shopdeail', 'POST', {
					shop_id: uni.getStorageSync("shop_id"),
				})
				if (res.code == 1) {
					uni.setStorageSync("appkey", res.data.appkey)
					uni.setStorageSync("hy_type", res.data.hy_type)
					uni.setStorageSync("opentable_status", res.data.opentable_status)
					uni.setStorageSync("is_sdkfp", res.data.is_sdkfp)
					uni.setStorageSync("is_call_sdkfp", res.data.is_call_sdkfp)
					uni.setStorageSync("is_pos_sdkfp", res.data.is_pos_sdkfp)
					uni.setStorageSync("houtel_type",res.data.houtel_type)
					this.data = res.data
				}
			},
			async getList() { //員工列表
				let res = await this.http.request('/api/shop/liststaff', 'POST', {
					shop_id: this.shop_id, //店鋪id
				})
				if (res.code == 1) {
					let arr = []
					res.data.forEach(item => {
						arr = []
						this.$set(item, "ischeck", false)
						this.listids.forEach(current => {
							if (current == item.id) {
								item.ischeck = true
							}
						})
						item.authority.split(",").forEach(value => {
							this.common.forEach(ite => {
								if (value == ite.id) {
									arr.push(ite)
								}
							})
						})
						this.$set(item, "authority_arr", arr)
					})
					this.list.data = res.data
				} else if (res.code == 0) {
					this.list.data = []
				} else {
					this.http.toast('系統處理中，請稍後再試');
				}
			},
			addStaff() { //添加員工
				if (this.data.staff_count == 0) {
					this.http.toast('無員工可用人數，正在前往購買頁面');
					uni.navigateTo({
						url: "/pages/workbench/renshugm"
					})
					return false
				}
				uni.navigateTo({
					url: "/pages/workbench/xinzengyg"
				})
			},
			editStaff(id) { //编辑員工
				uni.navigateTo({
					url: "/pages/workbench/xinzengyg?id=" + id
				})
			},
			removeStaff(id) { //刪除員工
				this.id = id
				this.modal.modalShow = true
			},
			modalCancel() { //取消
				this.modal.modalShow = false
			},
			async modalSure() { //确定刪除
				let res = await this.http.request('/api/shop/delstaff', 'POST', {
					id: this.id
				})
				if (res.code == 1) {
					this.http.toast("刪除成功")
					this.getList()
					this.get()
				} else if (res.code == 0) {
					this.http.toast(res.msg)
				} else {
					this.http.toast('系統處理中，請稍後再試');
				}
				this.modal.modalShow = false
			},
			submit() {
				let arr = []
				this.list.data.forEach(item => {
					if (item.ischeck) {
						arr.push(item.id)
					}
				})
				uni.$emit("checkyg", arr)
				uni.navigateBack(1)
			}
		}
	}
</script>

<style>

</style>